package com.github.shiqiyue.app.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.FastDateFormat;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.TimeZone;

/***
 * 时间工具类
 * @author wenyao.wu
 * @since 2020/7/23
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static final String HH_MM = "HH:mm";

    public static String format(Date date, String format) {
        return format(date, format, TimeZoneThreadLocal.getTimeZone());
    }

    public static String format(Date date, String format, TimeZone timeZone) {
        if (date == null) {
            return null;
        }
        return DateFormatUtils.format(date, format, timeZone);
    }

    public static Date parse(String dateStr, String format) throws ParseException {
        return parse(dateStr, TimeZoneThreadLocal.getTimeZone(), format);
    }

    public static Date parse(String dateStr, TimeZone timeZone, String format) throws ParseException {
        if (StringUtils.isBlank(dateStr)) {
            return null;
        }
        FastDateFormat fastDateFormat = FastDateFormat.getInstance(format, timeZone);
        return fastDateFormat.parse(dateStr);
    }


}
